package com.employee.crud;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class CrudApplication {

	private final Logger log = LoggerFactory.getLogger(CrudApplication.class);

	public static void main(String[] args) {
		SpringApplication.run(CrudApplication.class, args);
	}


	@Bean
	public CommandLineRunner runner(JdbcTemplate jdbcTemplate) {
		return args -> {
			log.info("Creating tables");
			jdbcTemplate.execute("drop table employee if exists");
			jdbcTemplate.execute("create table employee (id int generated by default as identity, name varchar(255), age int, gender varchar(255), primary key (id))");

			log.info("Insert Data");
			jdbcTemplate.execute("INSERT INTO employee (id,name, age,gender) VALUES " +
					" (0,'小明', 20,'男');");
			jdbcTemplate.execute("INSERT INTO employee (id,name, age,gender) VALUES " +
					" (1,'小红', 19,'女');");
			jdbcTemplate.execute("INSERT INTO employee (id,name, age,gender) VALUES " +
					" (2,'小智', 15,'男');");
			jdbcTemplate.execute("INSERT INTO employee (id,name, age,gender) VALUES " +
					" (3,'小刚', 16,'女');");
			jdbcTemplate.execute("INSERT INTO employee (id,name, age,gender) VALUES " +
					" (4,'小夏', 15,'女');");
		};
	}
}
